#include <AVUICommon.h>
#include <AVUIFrameworkElement.h>
#include <AVUISolidColorBrush.h>
#include <defs.h>
#include <GameWorld.h>

class CNetrekClientListener;

class Radar : public AVUI::FrameworkElement
{
public:

    Radar();

    DECLARE_ELEMENT(Radar, AVUI::FrameworkElement);

    void set_GameWorld(CGameWorld* pGameWorld) { m_pGameWorld = pGameWorld; }
    void set_ClientListener(CNetrekClientListener* pListener) { m_pListener = pListener; }

protected:

    virtual void OnRender(AVUI::DrawingContext& drawingContext);
    AVUI::Size MeasureOverride(const AVUI::Size& constraintSize);
    AVUI::Size ArrangeOverride(const AVUI::Size& constraintSize);

    virtual int get_VisualChildrenCount() AVUI_OVERRIDE;
    virtual AVUI::Visual* GetVisualChild(int idx) AVUI_OVERRIDE;

    virtual void OnMouseDown(AVUI::MouseButtonEventArgs* pArgs);

private:

    class PlanetVisual : public AVUI::FrameworkElement
    {
    public:
        CPlanet* pPlanet;
        AVUI::TRefCountedPtr<AVUI::SolidColorBrush> pBrush;
    };

    class PlayerVisual : public AVUI::FrameworkElement
    {
    public:
        CGamePlayer* pPlayer;
        AVUI::TRefCountedPtr<AVUI::SolidColorBrush> pBrush;
    };

    PlayerVisual* FindVisualForPlayer(CGamePlayer* pPlayer);
    PlanetVisual* FindVisualForPlanet(CPlanet* pPlanet);

    void OnGameWorldEvent(CGameWorldEvent* pGameWorldEvent);
    void EnsurePlanetVisuals();
    void EnsurePlayerVisuals();
    PlayerVisual* EnsurePlayerVisual(CGamePlayer* pPlayer);
    void DrawPlayerVisual(PlayerVisual* pVisual);
    void UpdatePlayerLocation(PlayerVisual* pVisual);
    void UpdatePlayerState(PlayerVisual* pVisual);
    void UpdatePlanetState(PlanetVisual* pVisual);

    bool m_isFirstRender;
    CNetrekClientListener* m_pListener;
    CGameWorld::CEventSubscription* m_pEventSubscription;
    DWORD GetPlanetColor(CPlanet* pPlanet);

    vector<AVUI::TRefCountedPtr<PlanetVisual> > m_planetVisuals;
    vector<AVUI::TRefCountedPtr<PlayerVisual> > m_playerVisuals;
    CGameWorld* m_pGameWorld;

    float m_flGalaxyToPixelsX;
    float m_flGalaxyToPixelsY;
};


